<%--
  Created by IntelliJ IDEA.
  User: 戴金睿
  Date: 2022/11/16
  Time: 13:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>EShopManager</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">


    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/static/lib/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/stylesheets/theme.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/lib/font-awesome/css/font-awesome.css"/>
    <script src="${pageContext.request.contextPath}/static/lib/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/tooltips.js"></script>
    <!-- Demo page code -->

    <style type="text/css">
        #line-chart {
            height: 300px;
            width: 800px;
            margin: 0px auto;
            margin-top: 1em;
        }

        .brand {
            font-family: georgia, serif;
        }

        .brand .first {
            color: #ccc;
            font-style: italic;
        }

        .brand .second {
            color: #fff;
            font-weight: bold;
        }
    </style>

    <script type="text/javascript">

        /**全选、全不选**/
        $(function () {
            $("#all_ck").change(function () {
                if ($(this).is(':checked')) {//全选
                    $("[name=ck]:checkbox").each(function () {
                        this.checked = true;
                        //$(this).attr("checked",true);
                    });
                } else {//全不选
                    $("[name=ck]:checkbox").each(function () {
                        this.checked = false;
                        //$(this).attr("checked",false);
                    });
                }

            });

        });

    </script>

</head>
<body>

<!-- 顶部 -->
<c:import url="top.jsp"></c:import>

<!-- 左边 -->
<c:import url="left.jsp?parent=图书管理&child=查看图书信息"></c:import>

<div class="content">
    <div class="header">
        <h1 class="page-title">图书管理</h1>
    </div>

    <ul class="breadcrumb">
        <li><a href="${pageContext.request.contextPath}/BookManagerController/select.do">图书管理</a> <span class="divider">/</span>
        </li>
        <li class="active">查看图书信息</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">

            <div class="block">
                <a href="#page-filter" class="block-heading" data-toggle="collapse">筛选</a>
                <div id="page-filter" class="block-body collapse in">
                    <div class="search-well">
                        <form class="form-inline" role="search"
                              action="select" method="post">
                            <%--                            <input type="hidden" name="method" value="select">--%>
                            <input type="text" class="form-control" placeholder="请输入书名"
                                   name="book_name" value="${param.book_name}"/>
                            <input type="text" class="form-control"
                                   placeholder="请输入作者" name="book_author" value="${param.book_author}"/>

                            <select class="form-control" name="status">
                                <option value="" ${param.status=="" ? "selected" : "" }>--请选择--</option>
                                <option value="1" ${param.status=="1" ? "selected" : "" }>上架</option>
                                <option value="0" ${param.status=="0" ? "selected" : "" }>下架</option>
                            </select>

                            <select class="form-control" name="type_id">

                                <option value="" ${param.type_id=="" ? "selected" : "" }>--请选择分类--</option>

                                <c:forEach items="${tbTypes}" var="tbType">
                                    <option value="${tbType.typeId}" ${param.type_id==tbType.typeId ? "selected" : "" }>
                                            ${tbType.typeName}
                                    </option>
                                </c:forEach>

                            </select>
                            <button type="submit" class="btn btn-default"><i class="icon-search"></i> 查询</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="row-fluid">

            <span>共有 <b>${bookInfos.pageCount}</b> 页记录</span>

            <div class="well">
                <div class="search-well">
                    <form class="form-inline" action="#">
                        <select name="control">
                            <option value="" selected="selected">--请选择--</option>
                            <option value="is_garbage-1">回收站</option>
                            <option value="status-1">上架</option>
                            <option value="status-0">下架</option>
                            <option value="is_new-1">新品</option>
                            <option value="is_new-0">取消新品</option>
                            <option value="is_hot-1">热卖</option>
                            <option value="is_hot-0">取消热卖</option>
                        </select>
                        <button id="btnUpdate" class="btn" type="button"><i class="icon-legal"></i> 操作</button>
                    </form>
                </div>


                <table class="table">
                    <thead>
                    <tr id="theTableTitle">
                        <th>
                            <input type="checkbox" id="all_ck"/>
                            编号
                        </th>
                        <th>书名</th>
                        <th>价格</th>
                        <th>上架</th>
                        <th>新品</th>
                        <th>热销</th>
                        <th>作者</th>
                        <th>所属分类</th>
                        <th>库存</th>
                        <th>操作</th>
                    </tr>
                    </thead>

                    <tbody>

                    <tr>
                        <c:forEach items="${bookInfos.bookInfo}" var="bookInfos">
                        <!-- 用临时变量解决复杂的循环 -->
                            <%-- <c:set var="index" value="${status.index}" />--%>
                        <td>
                            <input type="checkbox" name="ck" value="${bookInfos.bookId}"/>
                                ${bookInfos.bookId}
                        </td>
                        <td width="280px"> ${bookInfos.bookName}</td>
                        <td> ${bookInfos.bookPrice}</td>
                        <!-- 是否上架状态 -->
                        <c:choose>
                            <c:when test="${bookInfos.status eq 0}">
                                <td>
                                    <a href="javascript:updateIsOnline('${bookInfos.bookId}','status-1')"><img
                                            src="${pageContext.request.contextPath}/static/images/no.gif"></a>
                                </td>
                            </c:when>
                            <c:when test="${bookInfos.status eq 1}">
                                <td>
                                    <a href="javascript:updateIsOnline('${bookInfos.bookId}','status-0')"><img
                                            src="${pageContext.request.contextPath}/static/images/yes.gif"></a>
                                </td>
                            </c:when>
                        </c:choose>
                        <!-- 是新品否状态 -->
                        <c:choose>
                            <c:when test="${bookInfos.isNew eq 0}">
                                <td>
                                    <a href="javascript:updateIsNew('${bookInfos.bookId}','is_new-1')"><img
                                            src="${pageContext.request.contextPath}/static/images/no.gif"></a>
                                </td>
                            </c:when>
                            <c:otherwise>
                                <td>
                                    <a href="javascript:updateIsNew('${bookInfos.bookId}','is_new-0')"><img
                                            src="${pageContext.request.contextPath}/static/images/yes.gif"></a>
                                </td>
                            </c:otherwise>
                        </c:choose>
                        <!-- 是否热销 -->
                        <c:choose>
                            <c:when test="${bookInfos.isHot eq 0}">
                                <td>
                                    <a href="javascript:updateIsHot('${bookInfos.bookId}','is_hot-1')"><img
                                            src="${pageContext.request.contextPath}/static/images/no.gif"></a>
                                </td>
                            </c:when>
                            <c:otherwise>
                                <td>
                                    <a href="javascript:updateIsHot('${bookInfos.bookId}','is_hot-0')"><img
                                            src="${pageContext.request.contextPath}/static/images/yes.gif"></a>
                                </td>
                            </c:otherwise>
                        </c:choose>
                        <td> ${bookInfos.bookAuthor}</td>
                        <td> ${bookInfos.tbType.typeName}</td>
                        <td> ${bookInfos.storeCount}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/AddBookController?method=selectUpdateBook&book_id=${bookInfos.bookId}"
                               title="查看" target="_blank">
                                <i class="icon-search"></i>
                            </a>
                            <a href="${pageContext.request.contextPath}/AddBookController/selectUpdateBook?book_id=${bookInfos.bookId}"
                               title="编辑"><i class="icon-pencil"></i></a>
                            <a href="javascript:deleteClick(${bookInfos.bookId})" title="删除"><i class="icon-remove"></i></a>
                        </td>
                    </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>

            <!-- 控制返回来的page -->
            <c:set var="page" scope="request">
                <c:if test="${param.page==null}">
                    1
                </c:if>
                <c:if test="${param.page!=null}">
                    ${param.page}
                </c:if>
            </c:set>

            <!-- 保存当前查询状态 -->
            <c:set var="searchcondition" scope="request">
                ${pageContext.request.contextPath}/BookManagerController/select?book_name=${param.book_name}&author=${param.author}&status=${param.status}&type_id=${param.type_id}
            </c:set>

            <div class="pagination pagination myul">
                <ul>
                    <%--  active  disabled--%>
                    <%--是第一页--%>
                    <c:if test="${requestScope.page == 1}">
                        <li class='disabled'><a href="#">&laquo;首页</a></li>
                        <li class='disabled'><a href="#">上一页</a></li>
                    </c:if>
                    <%--不是第一页--%>
                    <c:if test="${requestScope.page != 1}">
                        <li class=''><a href="${requestScope.searchcondition}&page=1">
                            首页</a></li>
                        <li class=''><a href="${requestScope.searchcondition}&page=${requestScope.page-1}">
                            上一页</a></li>
                    </c:if>

                    <!-- 填充页数 -->
                        <c:forEach begin="1" end="${bookInfos.pageCount}" varStatus="loop">
                        <%--是当前页--%>
                        <c:if test="${requestScope.page == loop.index}">
                            <li class='active'>
                                <a href="javascript:">${loop.index}</a>
                            </li>
                        </c:if>
                        <%--不是当前页--%>
                        <c:if test="${requestScope.page != loop.index}">
                            <li class=''>
                                <a href="${requestScope.searchcondition}&page=${loop.index}">${loop.index}</a>
                            </li>
                        </c:if>
                    </c:forEach>

                    <!-- 最后一页 -->
                    <c:if test="${requestScope.page == bookInfos.pageCount}">
                        <li class='disabled'>
                            <a href="#">下一页</a>
                        </li>
                        <li class='disabled'>
                            <a href="#">尾页&raquo;</a>
                        </li>
                    </c:if>

                    <!-- 不是最后一页 -->
                    <c:if test="${requestScope.page != bookInfos.pageCount}">
                        <li>
                            <a href="${requestScope.searchcondition}&page=${requestScope.page+1}">
                                下一页</a>
                        </li>
                        <li>
                            <a href="${requestScope.searchcondition}&page=${bookInfos.pageCount}">
                                尾页&raquo;</a>
                        </li>
                    </c:if>

                </ul>
            </div>
        </div>
        <!-- 底部 -->
        <c:import url="footer.jsp"></c:import>
    </div>


</div>

<script src="${pageContext.request.contextPath}/static/lib/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    //$("[rel=tooltip]").tooltip();
    picUrl("${pageContext.request.contextPath}");

    function deleteClick(book_id) {
        //alert("==")

        if (confirm("你确定要移入商品回收站吗？")) {
            show_loading();
            //ajax请求servlet
            var url = "${pageContext.request.contextPath}/BookManagerController/deleteBook/" + book_id;

            $.post(url, function (data) {
                // var jsonData=$.parseJSON(response);
                // var result=jsonData.status;
                console.log(data)
                var responseUrl = "${requestScope.searchcondition}&page=${requestScope.page}";
                if (data.effectRow == 1) {
                    //跳转到responseUrl
                    show_msg('操作成功', responseUrl);
                } else {
                    show_err_msg('操作失败');
                }
            });
        }
    }

    function updateIsNew(book_id, control) {
        show_loading();
        //ajax请求servlet
        var url = "${pageContext.request.contextPath}/BookManagerController/updateBook";
        var data = {
            bookIds: book_id,
            control: control
        };

        $.post(url, data, function (response) {
            // var jsonData=$.parseJSON(response);
            // var result=jsonData.status;

            var responseUrl = "${requestScope.searchcondition}&page=${requestScope.page}";
            if (response.effectRow == 1) {
                show_msg('操作成功', responseUrl);
            } else {
                show_err_msg('操作失败');
            }

        });

    }

    function updateIsOnline(book_id, control) {

        show_loading();
        //ajax请求servlet
        var url = "${pageContext.request.contextPath}/BookManagerController/updateBook";
        var data = {
            bookIds: book_id,
            control: control     //字段名和控制变量
        };

        $.post(url, data, function (response) {
            // var jsonData=$.parseJSON(response);
            // var result=jsonData.status;

            var responseUrl = "${requestScope.searchcondition}&page=${requestScope.page}";
            if (response.effectRow == 1) {
                show_msg('操作成功', responseUrl);
            } else {
                show_err_msg('操作失败');
            }

        });

    }

    function updateIsHot(book_id, control) {
        show_loading();
        //ajax请求servlet
        var url = "${pageContext.request.contextPath}/BookManagerController/updateBook";
        var data = {
            bookIds: book_id,  //书本id
            control: control     //字段名和控制变量
        };

        $.post(url, data, function (response) {
            // var jsonData=$.parseJSON(response);
            // var result=jsonData.status;
            var responseUrl = "${requestScope.searchcondition}&page=${requestScope.page}";

            if (response.effectRow == 1) {
                show_msg('操作成功', responseUrl);
            } else {
                show_err_msg('操作失败');
            }

        });

    }

    $(function () {
        $('.demo-cancel-click').click(function () {
            return false;
        });

        $("#btnUpdate").click(function () {
            var control = $("select[name='control']").val();
            if (control == '') {
                show_err_msg("请选择您要的操作");
            } else {

                var ckValArray = [];
                $("input[name='ck']:checked").each(function () {
                    ckValArray.push($(this).val());
                });

                if (ckValArray.length == 0) {
                    show_err_msg("请选择编号");
                } else {
                    show_loading();
                    <!-- 选中复选框要操作的书本id，用逗号拼接成字符串 -->
                    var bookIds = ckValArray.join(",");
                    var url = "${pageContext.request.contextPath}/BookManagerController/updateBook";
                    var data = {
                        control: control, //包含了操作的字段名，和对应的值
                        bookIds: bookIds     //包含书本id
                    };

                    $.post(url, data, function (response) {
                        // var jsonData=$.parseJSON(response);
                        // var result=jsonData.status;
                        console.log(response);
                        var responseUrl = "${requestScope.searchcondition}&page=${requestScope.page}";
                        if (response.effectRow > 0) {
                            show_msg('操作成功', responseUrl);
                        } else {
                            show_err_msg('操作失败');
                        }
                    });
                }

            }
        });

    });
</script>

</body>
</html>
